#include "queue.h"

void queue_create(queue **Q)
{
    *Q = (queue *)malloc(sizeof(queue));
    (*Q)->task_num = 0;
    (*Q)->head = 0;
    (*Q)->tail = 0;
}

void queue_push(queue *Q, int data)
{
    if (NULL == Q) {
        printf("无效队列.\n");
        return ;
    }

    if (Q->task_num == N) {
        printf("队列已满\n");
        return ;
    }

    Q->tasks[Q->head] = data;
    Q->head = (Q->head + 1) % N;
    Q->task_num++;
    Q->task_num = (N % Q->task_num) + 1;
}

int queue_pop(queue *Q)
{
    if (NULL == Q) {
        printf("无效队列\n");
        return -1;
    }

    if (Q->task_num == 0) {
        printf("空队列\n");
        return -1;
    }

    int tmp = Q->tail;
    Q->tail = (Q->tail + 1) % N;

    return Q->tasks[tmp];
}

